--
-- Author: shunguo.chen
-- Date: 2016-08-09 13:53:57
--
-- FriendRender

local HeadRender = require("app.widget.renders.HeadRender")

local ClassRef = app.mvc.createRenderClass(...)

--data = FriendVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.FriendNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())

	-- head
	self._headRender = HeadRender.create(self._ui.head, 2)

	self._classIcon = self._ui["Panel/Panel_bg/icon2"]

	-- btns
	self._ui.Button_more:addClickEventListener(function()
		if self._data then
			if not UD:isMe(self._data.uid) then
				local pos = self._ui.Panel:convertToWorldSpace(cc.p(90,50))
				executeFunc(self._onBtnClick, "btnMore", {attr=self._data.info, pos=pos})
			else
				display.pushToast( L("tips_3000327") )
			end
		end
	end)

	self._ui.Panel_friend:getChildByName("Button_2"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "team", self._data)
	end)
	self._ui.Panel_friend:getChildByName("Button_3"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "speakTo", self._data)
	end)

	self._ui.Panel_add:getChildByName("Button_2"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "team", self._data)
	end)
	self._ui.Panel_add:getChildByName("Button_3"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "addFriend", self._data.uid)
	end)

	self._ui.Panel_black:getChildByName("Button_2"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "addFriend", self._data.uid)
	end)
	self._ui.Panel_black:getChildByName("Button_3"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "removeBlack", self._data.uid)
	end)

	self._ui.Panel_apply:getChildByName("Button_2"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "refruseAddFriend", self._data)
	end)
	self._ui.Panel_apply:getChildByName("Button_3"):addClickEventListener(function()
		executeFunc(self._onBtnClick, "agreeAddFriend", self._data)
	end)

	local qqBtn1 = self._ui["Panel/btn_qq_1"]
	local qqBtn2 = self._ui["Panel/btn_qq_2"]
	local wxBtn = self._ui["Panel/btn_wx"]
	qqBtn1:addClickEventListener(function()
		UD.msdkData:qqGameCenterClickTip()
	end)
	qqBtn2:addClickEventListener(function()
		UD.msdkData:qqVipClickTip()
	end)
	wxBtn:addClickEventListener(function()
		UD.msdkData:wxGameCenterClickTip()
	end)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self:setVisible(true)

		self._ui.Panel_friend:setVisible(self._data:isFriend())
		self._ui.Panel_black:setVisible(self._data:isBlack())
		self._ui.Panel_add:setVisible(self._data:isStranger())
		self._ui.Panel_apply:setVisible(self._data:isApply())

		self:setName(self._data.name)
		self:setOnline(self._data.online)
		self:setHead(self._data)

		self._ui.combat_num:setString(self._data.combatPower)

		self._ui.Text_guild:setString(self._data:getDuty())

		self._classIcon:loadTexture(self._data:getSpecialistIcon())


		local qqBtn1 = self._ui["Panel/btn_qq_1"]
		local qqBtn2 = self._ui["Panel/btn_qq_2"]
		local wxBtn = self._ui["Panel/btn_wx"]
	
		local wakeUp = rawget(self._data, "wakeUpFrom")
		if not wakeUp then
			wakeUp = Enums.WAKE_UP.NONE
		end
		qqBtn1:setVisible(UD.msdkData:isWakeUpFromQQGame(wakeUp))
		wxBtn:setVisible(UD.msdkData:isWakeUpFromWXGame(wakeUp))
		qqBtn2:setVisible(false)
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

function ClassRef:setName(name)
	self._ui.Text_name:setString(name)
end

function ClassRef:setOnline(b)
	self._ui.mask:setVisible(not b)
end

function ClassRef:setJob(job)
	local path = GD:getClassIconPath(job, 2)
	self._ui["player/playericon/zhiye"]:loadTexture(path)
end

function ClassRef:setHead(data)
	self._headRender:setData(data)
end

function ClassRef:setGift(b)
	-- self._ui.gift:setVisible(b)
end

return ClassRef